/* Bfgeshka, 2021
 * Made for version 3.3.2 */
/* Desired stonescript code line is this long */
/* https://stonestoryrpg.com/stonescript/manual.html */

var Utils = import Custom/BUtils.stone
var Stats = import Custom/Stats.stone

/* Config */
var ourbdist = 23
var dashdist = 10
var armbuffmax = 4
var barddmg = 724
var bladedmg = 165

var defVigorMelee = "makes me stronger"

var wandVigor = "wand dL"
//var wandPoison = "wand dP"
var wandIce = "wand dI"
var wandFire = "wand dF"
var wandAether = "wand aether D"

var knifeVigor = "sword dL shiny"
var knifeAether = "sword aether D"
var knifePoison = "poisoned knife"
var knifeFire = "sword dF"
var knifeIce = "sword dI"

var shieldMax = "compound shield"
var shieldVigor = "what doesnt kill me"
var shieldAether = "hammer au"
var shieldIce = "shield ai"
var shieldPoison = "shield ap"

var foeLast = ""
var foeDBice = 0
var foeDBfire = 0
var foeDBpoison = 0
var foeBvigor = 0

var bossAttacksInARow = 0
var nodash = false

func RefreshBossPhase ()
	? !foe | foeLast ! foe
		bossAttacksInARow = 0

func GetBladeThresh ()
	? loc = mine
		return 4
	:
		return 5

func CountFoeOnlyDB ()
	var buffsarr = Utils.GetBuffArrFull(foe.debuffs.string)
	? !buffsarr | buffsarr.Count() = 0
		return
	for i = 0..buffsarr.Count() - 1
		? buffsarr[i] = "∞"
			foeDBpoison = int.Parse(buffsarr[i+2])
		? buffsarr[i] = "❄"
			foeDBice = int.Parse(buffsarr[i+2])
		? buffsarr[i] = "φ"
			foeDBfire = int.Parse(buffsarr[i+2])
		i++
		i++

func CountFoeOnlyB ()
	var buffsarr = Utils.GetBuffArrFull(foe.buffs.string)
	? !buffsarr | buffsarr.Count() = 0
		return
	for i = 0..buffsarr.Count() - 1
		? buffsarr[i] = "❤"
			foeBvigor = int.Parse(buffsarr[i+2])
		i++
		i++

func CountFoeBuffs ()
	foeDBice = 0
	foeDBfire = 0
	foeDBpoison = 0
	foeBvigor = 0
	? !foe
		return
	? foe.debuffs.count > 0
		CountFoeOnlyDB()
	? foe.buffs.count > 0
		CountFoeOnlyB()

func EqArm ()
	? foe.distance >= 7 | !foe | foe = immune_to_physical
		return false
	equip skeleton_arm
	return true

func EqMind ()
	? item.GetCooldown("mind") < 1
		equipL mind stone
		return true
	return false

func EqDash ()
	? item.GetCooldown("dash") < 1 & !nodash
		equipR dashing shield
		return true
	return false

func EqBash ()
	? item.GetCooldown("bash") < 1 & !nodash
		equipR bash
		return true
	return false

func EqQstaff ()
	? item.GetCooldown("quarterstaff") < 1
		? item.CanActivate()
			equip quarterstaff
			activate R
			return true
	return false

func EqMeleeDps ()
	var lefteq = knifePoison
	var righteq = knifeFire
	? hp < maxhp
		lefteq = knifeVigor
	? foeDBice < 6 & foeBvigor = 0
		righteq = knifeIce

	equipL @lefteq@
	equipR @righteq@
	return

func EqRangedDps ()
	equipL @wandIce@
	equipR @wandFire@
	return

func EqShield ()
	? foe.count = 0
		equipR @shieldMax@
		return
	? foe = dysangelos
		equipR @shieldVigor@
		return
	? foe = boss
		? hp < maxhp * 0.8
			equipR @shieldVigor@
		? foeDBice = 0
			equipR @shieldIce@
		? foeDBpoison = 0 & foe.state = 32
			equipR @shieldPoison@
		return

	?  foeDBice = 0 & foe.state ! 32
		equipR @shieldIce@
//	:? foeDBpoison = 0 & foe.state = 32
//		equipR @shieldPoison@
	:? hp < maxhp * 0.8
		equipR @shieldVigor@
	:
		equipR @shieldAether@

func EqHammers ()
	? foe = bronze
		? foeDBice < 5
			equipL hammer aether D
			equipR hammer dI
		:
			equipL hammer aether D
			equipR hammer dF
	? foe = dysangelos
		equipL hammer dF
		equipR hammer dI
	return

func CheckBlade ()
	? item.GetCooldown("blade") > 0 | !item.CanActivate() | !ai.enabled | foe.distance > 25
		return
	? foe = pallas
		return
	? foe.count < GetBladeThresh() & foe ! boss
		return
	? foe = dysangelos
		? ( foe = phase1 | foe = phase2 ) & foe.hp > bladedmg
			return
	? loc = rocky & foe ! dysangelos & foe.hp > bladedmg
		return
	equip blade
	activate R
	return

func CheckBardiche ()
	? item.GetCooldown("bardiche") > 0 | foe.distance > 9 | foe.hp < 1
		return
	? foe.armor > 0 & foe.armor + foe.hp >= barddmg
		return
	? foe = boss & foe.hp < 200
		return
	? foe.state = 32
		return
	equip bardiche
	activate R
	return

func LCrypt ()
	? foe = immune_to_physical
		? foe.distance < 10 & !EqMind()
			equipL @wandVigor@
	:? foe.distance > 10
			equipL @wandVigor@
	:
		equipL @defVigorMelee@
	? item.left = wand
		equipR @wandFire@
	:
		EqShield()

func LCryptChallengeTitan4 ()
		equipL @wandVigor@
		equipR @wandFire@

func LPallas ()
	? foe ! boss
		equipL @wandVigor@
		EqShield()
	:
		equipL @knifeVigor@
		equipR @defVigorMelee@
		? foe = phase1
			? foe.state = 32 & foe.time > 50
				EqShield()
		:
			? foe.count > 1
				equipL @wandVigor@
			:
				equipL @knifeVigor@
			? foe.state = 32
				EqShield()
			:
				? foe.distance > 9
					equipR @wandFire@
				:
					equipR @defVigorMelee@
		? foe.distance > 9
			? !EqBash()
				? !EqDash()
					EqShield()

func LPallasChallengeTitan4 ()
		equipL @wandVigor@
		equipR @wandFire@
	? foe ! boss
		EqShield()
	:
		? foe = phase1
			? foe.state = 32 & foe.time > 50
				EqShield()
		:
			? foe.state = 32
				EqShield()

func LFungus ()
	equipL poison big sword
	EqShield()
	? foe = boss
		? foe.armor > 0
			equipL hammer poison
		:
			equipL @knifePoison@
		? foe.state = 32 & Stats.foeStateFrames > 40
			? !EqMind()
				EqShield()

func LTemple ()
	? Stats.smiteBuffs = 0
		equipL @wandIce@
	:
		equipL @knifeIce@
	EqShield()

func LTempleChallengeTitan4 ()
	equipL @wandIce@
	equipR @wandFire@

func LNagaraja ()
	equipL @knifeIce@
	equipR @knifeFire@
	? foe.state = 112 & Stats.foeStateFrames > 55 & ( item.left = sword | item.right = sword )
		EqMind()

func LNagarajaChallengeTitan4 ()
	? Stats.frame % 120 < 60 | Stats.smiteBuffs = 0
		equipL @knifeIce@
		equipR @knifeFire@
	:
		equipL @knifePoison@
		equipR @knifeFire@
	? foe.state = 112 & Stats.foeStateFrames > 55 & ( item.left = sword | item.right = sword )
		EqMind()

func LValley ()
	? foe = boss
		EqMeleeDps()
		? foe.state = 32 & Stats.foeStateFrames > 40
			? !EqMind()
				EqShield()
		? foe.state = 32 & Stats.foeStateFrames > 50
			? !EqBash()
				? !EqDash()
					? EqShield()
	:
		equipL big dP sword
		EqShield()

func LRidge ()
	equipL @wandFire@
	EqShield()
	? foe = boss
		EqMeleeDps()
		? foe.state = 133
			equipR @wandVigor@
			equipL @wandFire@
		? foe.state < 2
			EqMeleeDps()
//		? foe.state = 33 & Stats.foeStateFrames < 10 /* ranged */
		? ( foe.state = 32 & Stats.foeStateFrames > 20 ) | ( foe.state = 33 & Stats.foeStateFrames < 5 ) /* melee */
			EqShield()

func LMines ()
	EqShield()
	? hp < maxhp
		equipL @wandVigor@
	:
		equipL @wandAether@
	? foe = bomb cart
		equip repeating crossbow

func LBronzeGuard ()
	equipL @wandAether@
	equipR @wandFire@
	? foe.state = 32 & Stats.foeStateFrames > 30 & bossAttacksInARow < 4
		? !EqMind()
			EqShield()
	? foe.state = 33
		? foe.armor > 0
			EqHammers()
		:
			EqMeleeDps()

func LCaves ()
	equipL big dP sword
	EqShield()
	? foe = boss
		EqMeleeDps()
		equipL @knifeIce@
		? foe.state = 142 & Stats.foeStateFrames > 50
			? !EqMind()
				EqShield()

func LDysangelosCyan ()
	EqMeleeDps()
	? foe ! boss & foe.state = 32
		EqShield()
	?  foe = phase1
		? foe.state = 33 & bossAttacksInARow % 6 = 0
			? Stats.foeStateFrames = 21
				EqMind()
			? Stats.foeStateFrames = 22
				? !EqBash()
					? !EqDash()
						EqShield()
	:? foe = phase2
		?  foe = fire
			equipL @knifeAether@
		:? foe = ice
			equipL @knifeFire@
		:? foe = poison
			equipL @knifeIce@
		:? foe = aether
			equipL @knifeVigor@
		:? foe = vigor
			equipL @knifePoison@
		? hp < maxhp * 0.5
			equipL @knifeVigor@
	:? foe = phase3
		? foe.state > 100
			bossAttacksInARow = 0
		? foe.state = 32 & bossAttacksInARow % 3 = 0
			nodash = true
			? ( foeDBice = 0 & Stats.foeStateFrames > 25 ) | ( foeDBice > 0 & Stats.foeStateFrames > 30 )
				EqMind()
				equipR @wandFire@
		? foe.armor > 0
			EqHammers()
		? foe.state = 115
			? Stats.foeStateFrames > 45
				EqMind()
				equipR @wandFire@
	? ( foe.state = 32 & Stats.foeStateFrames > 45 )
		EqShield()

func LDysangelosYellow ()
	EqMeleeDps()
	? foe ! boss & foe.state = 32
		EqShield()
	?  foe = phase1
		? foe.state = 33 & bossAttacksInARow % 16 = 0
			? Stats.foeStateFrames = 20
				EqMind()
			? Stats.foeStateFrames = 22
				? !EqBash()
					? !EqDash()
						EqShield()
		? ( foe.state = 32 & Stats.foeStateFrames > 22 )
			EqShield()
	:? foe = phase2
		?  foe = fire
			equipL @knifeAether@
		:? foe = ice
			equipL @knifeFire@
		:? foe = poison
			equipL @knifeIce@
		:? foe = aether
			equipL @knifeVigor@
		:? foe = vigor
			equipL @knifePoison@
		? hp < maxhp * 0.5
			equipL @knifeVigor@
		? foe.state = 32 & Stats.foeStateFrames > 55
			EqShield()
	:? foe = phase3
		? foe.state > 100
			bossAttacksInARow = 0
		? foe.armor > 0
			EqHammers()
		? foe.state = 32 & bossAttacksInARow % 3 = 0 & foe.distance < 10
			nodash = true
			? ( foeDBice = 0 & Stats.foeStateFrames > 27 ) | ( foeDBice > 0 & Stats.foeStateFrames > 60 )
				EqMind()
				equipR @wandFire@
		? foe.state > 110 & foe.state < 115
			nodash = true
			EqRangedDps()
		? foe.state = 112
			EqMind()
		? foe.state = 115
			? Stats.foeStateFrames < 30
				nodash = true
				EqRangedDps()
			? Stats.foeStateFrames > 50 & foe.distance > 6
				? !EqBash()
					? !EqDash()
						EqShield()
		? foe.state = 32 & (( foeDBice = 0 & Stats.foeStateFrames > 45 ) | ( foeDBice > 0 & Stats.foeStateFrames > 60 ))
			EqShield()

//Stats.OnUpdate()
nodash = false
CountFoeBuffs()
RefreshBossPhase ()
>`@screen.w - 5@,@screen.h - 5@,@bossAttacksInARow@

? foe = boss & foe.state = 32 & Stats.foeStateFrames = 1
	bossAttacksInARow++

? item.GetCooldown("bardiche") > 877
	return

? !ai.enabled | ai.paused
	equipL triskelion
	equipR @shieldMax@
	return

/* Autopotion, tar mix */
? hp < maxhp / 4
	activate potion

/* Farming exploring loadouts */
EqMeleeDps()
? loc = caustic_caves
//	? foe.distance < 23 & foe.distance > 20
//		equip repeating crossbow
//	:
		LCaves()
:? loc = deadwood_valley
	LValley()
:? loc = pallas
	LPallas()
:? loc = undead_crypt | loc = wildride_Pallas
	LCrypt()
:? loc = bronze guardian
	LBronzeGuard()
:? loc = bronze_mine
	LMines()
:? loc = icy_ridge
	LRidge()
:? loc = fungus_forest
	LFungus()
:? loc = nagaraja
	LNagaraja()
:? loc = temple
	LTemple()
:? loc = rocky
	? loc.stars < 11
		LDysangelosCyan()
	:
		LDysangelosYellow()

/*
? Stats.armBuffs < armbuffmax &
^ foe ! ranged &
^ foe ! immune_to_physical &
^ foe.level < 5
	EqArm()
*/

/*
? item.GetCooldown("skeleton_arm") = 0 &
^ item.CanActivate("potion") &
^ Stats.armBuffs = armbuffmax &
^ foe ! spawner
	EqArm()
	? item.CanActivate("skeleton_arm")
		activate R
*/

CheckBardiche()
CheckBlade()

? ( !foe | foe.state = 0 | foe.state = 1 ) & foe.distance > ourbdist
	equipL triskelion
	equipR @shieldMax@

? !foe | foe.distance > 30
	equipL triskelion
	equipR @shieldMax@

? ( !foe | ( foe ! boss & foe.distance > dashdist ) )
	EqQstaff()

? ( item.left ! wand & item.left ! crossbow ) &
^ foe.distance > 9 & foe.distance < 19 &
^ foe ! immune_to_physical & foe ! bomb cart
	? !EqBash()
		EqDash()

? pickup & pickup.distance < 10
	equipR star stone

foeLast = foe
